/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.core.headers;

import java.io.IOException;
import java.util.Properties;
import java.util.Random;

import io.gitee.huangjianbo.supos.openapi.core.config.SuposApiClientConfig;
import io.gitee.huangjianbo.supos.openapi.core.http.HttpContentType;
import io.gitee.huangjianbo.supos.openapi.core.http.HttpHeader;
import io.gitee.huangjianbo.supos.openapi.core.http.HttpHeaders;
import io.gitee.huangjianbo.supos.openapi.core.thread.ClientContext;
import io.gitee.huangjianbo.supos.openapi.core.thread.ClientContextHolder;
import io.gitee.huangjianbo.supos.openapi.core.tools.DateTimeUtils;
import io.gitee.huangjianbo.supos.openapi.core.tools.IdUtils;
import io.gitee.huangjianbo.supos.openapi.core.tools.StringUtils;

/**
 * @author tomcat
 * @version 1.0
 */
public final class CommonHeaders {

    /**
     * 获取SDK版本
     *
     * @return 版本号
     */
    public static String getBuildVersion() {
        Properties properties = new Properties();
        try {
            properties.load(CommonHeaders.class.getClassLoader().getResourceAsStream("build-info.properties"));
            return String.valueOf(properties.getOrDefault("version", "unknown"));
        } catch (IOException e) {
            return "unknown";
        }
    }

    /**
     * 获取默认请求头
     *
     * @param config 客户端配置
     * @return 默认请求头
     */
    public static HttpHeader getDefaultHeaders(SuposApiClientConfig config) {
        return HttpHeader.of()
                .add(HttpHeaders.X_SERVICE_NAME, config.getServiceName())
                .add(HttpHeaders.X_SERVICE_IP, config.getServiceIp())
                .add(HttpHeaders.X_SERVICE_ID, config.getAppId());
    }

    /**
     * 获取此次请求的请求头
     *
     * @param config 客户端配置
     * @return 请求头
     */
    public static HttpHeader getRequestHeaders(SuposApiClientConfig config) {
        ClientContext context = ClientContextHolder.getContext();
        HttpHeader httpHeader = HttpHeader.of();
        // Accept-Language
        httpHeader.add(HttpHeaders.ACCEPT_LANGUAGE, config.getDefaultLanguage().getAcceptLanguage());
        if (context != null && context.getLanguage() != null) {
            httpHeader.add(HttpHeaders.ACCEPT_LANGUAGE, context.getLanguage().getAcceptLanguage());
        }
        // X-Tenant-Id
        httpHeader.add(HttpHeaders.X_TENANT_ID, config.getTenantId());
        if (context != null && StringUtils.isNotEmpty(context.getTenantId())) {
            httpHeader.add(HttpHeaders.X_TENANT_ID, context.getTenantId());
        }
        // X-MC-Date
        httpHeader.add(HttpHeaders.X_MC_DATE, DateTimeUtils.getSimpleUTC0());
        // X-Trace-Id
        httpHeader.add(HttpHeaders.X_TRACE_ID, Long.toString(IdUtils.getNanoId()));

        return httpHeader;
    }

    /**
     * 构建multipart/form-data的请求头
     *
     * @return 返回带boundary的头数据<br> 示例：multipart/form-data;charset=UTF-8;boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
     */
    public static HttpHeader buildMultipartContentType() {
        StringBuilder sb = new StringBuilder(HttpHeaders.BOUNDARY_PREFIX);

        Random random = new Random();
        for (int i = 0; i < 15; i++){
            sb.append(HttpHeaders.BOUNDARY_CHARS[random.nextInt(HttpHeaders.BOUNDARY_CHARS.length)]);
        }

        String headerValue = HttpContentType.MULTIPART_FORM_DATA_BOUNDARY + HttpHeaders.BOUNDARY_PREFIX + sb.toString();

        return HttpHeader.of(HttpHeaders.CONTENT_TYPE, headerValue);
    }
}
